package com.yic.module.wms.service.itemwarehouse;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.system.enums.dict.SystemBooleanEnum;
import com.yic.module.system.enums.dict.WmsInventoryTypeEnum;
import com.yic.module.wms.dal.dataobject.warehouse.WarehouseDO;
import com.yic.module.wms.service.warehouse.WarehouseService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import com.yic.module.wms.controller.admin.itemwarehouse.vo.*;
import com.yic.module.wms.dal.dataobject.itemwarehouse.ItemWarehouseDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.wms.convert.itemwarehouse.ItemWarehouseConvert;
import com.yic.module.wms.dal.mysql.itemwarehouse.ItemWarehouseMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.wms.enums.ErrorCodeConstants.*;

/**
 * 产品仓储信息 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ItemWarehouseServiceImpl implements ItemWarehouseService {

    @Resource
    private ItemWarehouseMapper itemWarehouseMapper;
    @Resource
    private WarehouseService warehouseService;

    @Override
    public Long createItemWarehouse(ItemWarehouseCreateReqVO createReqVO) {
        validateCreateOrUpdate(null, createReqVO.getFactoryId(), createReqVO.getItemId());
        // 插入
        ItemWarehouseDO itemWarehouse = ItemWarehouseConvert.INSTANCE.convert(createReqVO);
        itemWarehouseMapper.insert(itemWarehouse);
        // 返回
        return itemWarehouse.getId();
    }

    @Override
    public void updateItemWarehouse(ItemWarehouseUpdateReqVO updateReqVO) {
        // 校验存在
        validateCreateOrUpdate(updateReqVO.getId(), updateReqVO.getFactoryId(), updateReqVO.getItemId());
        // 更新
        ItemWarehouseDO updateObj = ItemWarehouseConvert.INSTANCE.convert(updateReqVO);
        itemWarehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteItemWarehouse(Long id) {
        // 校验存在
        validateItemWarehouseExists(id);
        // TODO 删除校验
        // 删除
        itemWarehouseMapper.deleteById(id);
    }

    private void validateCreateOrUpdate(Long id, Long factoryId, Long itemId) {
        validateItem(id, factoryId, itemId);
    }

    private void validateItem(Long id, Long factoryId, Long itemId) {
        ItemWarehouseDO warehouseDO = itemWarehouseMapper.selectByFactoryAndItem(factoryId, itemId);
        if (ObjectUtil.isEmpty(warehouseDO)) {
            return;
        }
        if (id == null) {
            throw exception(ITEM_WAREHOUSE_EXISTS);
        }
        if (!id.equals(warehouseDO.getId())) {
            throw exception(ITEM_WAREHOUSE_EXISTS);
        }
    }

    private ItemWarehouseDO validateItemWarehouseExists(Long id) {
        ItemWarehouseDO warehouseDO = itemWarehouseMapper.selectById(id);
        if (warehouseDO == null) {
            throw exception(ITEM_WAREHOUSE_NOT_EXISTS);
        }
        return warehouseDO;
    }

    @Override
    public ItemWarehouseDO getItemWarehouse(Long id) {
        return itemWarehouseMapper.selectById(id);
    }

    @Override
    public List<ItemWarehouseDO> getItemWarehouseList(Collection<Long> ids) {
        return itemWarehouseMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ItemWarehouseDO> getItemWarehousePage(ItemWarehousePageReqVO pageReqVO) {
        return itemWarehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ItemWarehouseDO> getItemWarehouseList(ItemWarehouseExportReqVO exportReqVO) {
        return itemWarehouseMapper.selectList(exportReqVO);
    }

    @Override
    public ItemWarehouseDO getItemWarehouse(Long factoryId, Long itemId) {
        return itemWarehouseMapper.selectByFactoryAndItem(factoryId, itemId);
    }

    @Override
    public void updateItemWarehouse(ItemWarehouseDO itemWarehouseDO) {
        itemWarehouseMapper.updateById(itemWarehouseDO);
    }

}
